#!/bin/bash
echo "此脚本用来安装stel ref debug语音包，安装在/system/priv-app/BaiduVoice/目录下  @auther 李鹏程"
echo "此脚本用来安装stel ref debug语音包，安装在/system/priv-app/BaiduVoice/目录下  @auther 李鹏程"
echo "此脚本用来安装stel ref debug语音包，安装在/system/priv-app/BaiduVoice/目录下  @auther 李鹏程"

adb devices
adb root && adb remount && adb shell setenforce 0

# apk的包名
packagename="com.baidu.che.codriver"
# apk路径
apkPath=$(ls ./auto-app/hmi-ref/build/outputs/apk/debug/DuerOS_RefApp_debug_*.apk)
echo "apkPath=$apkPath"
# apk名称
apkFileName=$(basename $apkPath)
echo "apkFileName=$apkFileName"
# 缓存解压目录
apkCachePath=$(dirname $apkPath)/$(basename $apkFileName .apk)/
echo "apkCachePath=$apkCachePath"
# apk.zip路径
apkZipPath=${apkCachePath}/${apkFileName}.zip
echo "apkZipPath=$apkZipPath"
# 备份目录
backupDir=~/Desktop/apk_stel_$(date "+%Y-%m-%d-%H_%M_%S")/
echo "backupDir=$backupDir"
# 系统apk目录
apkSysDirPath=/system/priv-app/BaiduVoice/
echo "apkSysDirPath=$apkSysDirPath"
# 系统lib目录
apkLibSysDirPath=$apkSysDirPath/lib/arm64/

echo "一、备份，回车跳过此步骤！"
echo "1.备份apk"
echo "2.备份apk和vrres"
echo "3.push lib-resources资源到车机下"
echo "4.2+3"
read -p "请输入序号:" type;
if [[ $type == 1 ]];then
  echo "备份apk到桌面"
  if [ ! -f $backupDir ];then
    mkdir -p ${backupDir}
  fi
  adb pull /system/priv-app/BaiduVoice/ ${backupDir}
elif [[ $type == 2 ]]; then
  echo "备份apk和vrres到桌面"
  if [ ! -f $backupDir ];then
    mkdir -p ${backupDir}
  fi
  adb pull /system/priv-app/BaiduVoice/ ${backupDir}
  adb pull /mnt/user/10/emulated/10/ivres/vrres/ ${backupDir}
elif [[ $type == 3 ]]; then
  echo "push lib-resources资源到车机下"
  adb shell rm -rf /mnt/user/10/emulated/10/ivres/vrres/
  adb push ./lib-resources/vrres /mnt/user/10/emulated/10/ivres/
elif [[ $type == 4 ]]; then
  echo "备份apk和vrres到桌面,然后push lib-resources资源到车机下"
  if [ ! -f $backupDir ];then
    mkdir -p ${backupDir}
  fi
  adb pull /system/priv-app/BaiduVoice/ ${backupDir}
  adb pull /mnt/user/10/emulated/10/ivres/vrres/ ${backupDir}
  adb shell rm -rf /mnt/user/10/emulated/10/ivres/vrres/
  adb push ./lib-resources/vrres /mnt/user/10/emulated/10/ivres/
else
  echo "不备份，跳过此步骤"
fi

echo "二、push /system/priv-app/BaiduVoice/安装"
echo "1.clear apk"
array=("$packagename")
for(( i=0;i<${#array[@]};i++)) do
	echo ${array[i]};
	adb shell pm clear ${array[i]}
	adb uninstall ${array[i]}
	#adb shell rm -rf /data/app/com.baidu.che.codriver
	dataAppFilePath=$(adb shell find /data/app/ -name "*${array[i]}*")
	echo "dataAppFilePath:${dataAppFilePath}"
	adb shell rm -rf $dataAppFilePath
	adb shell rm -rf /data/data/${array[i]}
done;
adb shell rm -rf $apkSysDirPath
adb shell mkdir -p $apkLibSysDirPath

#根据输入参数判断apk、lib路径
originDirPath=$1
if [[ -n $originDirPath && -d $originDirPath ]];then
  echo "${originDirPath} is dir"
  echo "2.push lib"
  adb push $originDirPath/BaiduVoice/lib/arm64/* $apkLibSysDirPath

  echo "3.push apk And res"
  originApkPath=$(ls $originDirPath/BaiduVoice/*.apk)
  adb push $originApkPath $apkSysDirPath
  echo "clear res And push res again"
  adb shell rm -rf /mnt/user/10/emulated/10/ivres/vrres/
  adb push $originDirPath/vrres /mnt/user/10/emulated/10/ivres/
else
  #待验证
  if [[ -n $originDirPath && -f $originDirPath ]];then
    apkPath=$originDirPath
    echo "apkPath=$apkPath"
    # apk名称
    apkFileName=$(basename $apkPath)
    echo "apkFileName=$apkFileName"
    # 缓存解压目录
    apkCachePath=$(dirname $apkPath)/$(basename $apkFileName .apk)/
    echo "apkCachePath=$apkCachePath"
    # apk.zip路径
    apkZipPath=${apkCachePath}/${apkFileName}.zip
    echo "apkZipPath=$apkZipPath"
  fi

  echo "originDirPath:${originDirPath} is not dir"
  echo "2.push lib"
  rm -rf $apkCachePath
  mkdir -p $apkCachePath
  cp $apkPath $apkZipPath
  unzip -o $apkZipPath  -d $apkCachePath
  adb push $apkCachePath/lib/arm64-v8a/* $apkLibSysDirPath

  echo "3.push apk"
  adb push $apkPath $apkSysDirPath
fi

echo "4.reboot"
adb reboot
adb wait-for-device
adb devices
adb root && adb remount && adb shell setenforce 0
sleep 3
adb shell pm dump $packagename |grep versionName
adb shell pm dump $packagename |grep path

echo "5.permission"
adb shell pm grant --user 10 com.baidu.che.codriver android.permission.SYSTEM_ALERT_WINDOW
adb shell pm grant --user 10 com.baidu.che.codriver android.permission.READ_EXTERNAL_STORAGE
adb shell pm grant --user 10 com.baidu.che.codriver android.permission.WRITE_EXTERNAL_STORAGE
adb shell pm grant --user 10 com.baidu.che.codriver android.permission.RECORD_AUDIO
adb shell pm grant --user 10 com.baidu.che.codriver android.permission.CALL_PHONE
adb shell pm grant --user 10 com.baidu.che.codriver android.permission.READ_CONTACTS
adb shell pm grant --user 10 com.baidu.che.codriver android.permission.READ_PHONE_STATE
adb shell pm grant --user 10 com.baidu.che.codriver android.permission.READ_CALL_LOG
adb shell pm grant --user 10 com.baidu.che.codriver android.permission.ACCESS_COARSE_LOCATION
adb shell pm grant --user 10 com.baidu.che.codriver android.permission.ACCESS_FINE_LOCATION